package com.easyattendance.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.easyattendance.model.AllTimeSlots;
import com.easyattendance.model.TimeSlot;
import com.easyattendance.model.TimeTable;
import com.easyattendance.util.HibernateUtil;

public class AddTimeSlotsSettingsService {

	/* Method to CREATE a timeslot in the database */
	public synchronized boolean addTimeSlotSettings(TimeSlot timeSlot) {
		Session session = HibernateUtil.openSession();
		Transaction tx = null;
		// boolean result=false;

		try {
			tx = session.beginTransaction();

			session.save(timeSlot);
			tx.commit();

		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return true;
	}
	
	 

	/* Method to READ all the timeslot values */
	public List listTimeSlotsSettings() {
		Session session = HibernateUtil.openSession();
		Transaction tx = null;
		List timeSlotSettings = null;
		try {
			tx = session.beginTransaction();
			timeSlotSettings = session.createQuery("FROM TimeSlot").list();
			for (Iterator iterator = timeSlotSettings.iterator(); iterator.hasNext();) {
				TimeSlot TimeSlot = (TimeSlot) iterator.next();
			}
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return timeSlotSettings;
	}
	
	 public int getTimeslotIdValue()
	   {
		   	Session session = HibernateUtil.openSession();
	        Transaction tx = null;
	        int value=0;
	        try {
	            tx = session.getTransaction();
	            tx.begin();
	            SQLQuery query = session.createSQLQuery("select MAX(time_slot_id) from tbl_timeslot");
	           
	            if(query.uniqueResult()!=null)
	            {
	            	String timeslot_id=(String) query.uniqueResult();
	            	String timeslot_counter=timeslot_id.substring(9);
	            	value = Integer.parseInt(timeslot_counter)+1;
	            }
	            else
	            	value=1;
	            
	            tx.commit();
	        } catch (Exception e) {
	            if (tx != null) {
	                tx.rollback();
	            }
	            e.printStackTrace();
	        } finally {
	            session.close();
	        }
		   
		   return value;
	   }
	
	public boolean ifAlreadyExists(TimeSlot newTimeSlot) {
		 boolean flag = false;
		 
		 Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List timeSlots = null;
	      try{
	         tx = session.beginTransaction();
	         timeSlots = session.createQuery("FROM TimeSlot").list(); 
	         for (Iterator iterator = timeSlots.iterator(); iterator.hasNext();){
	        	 TimeSlot timeSlot = (TimeSlot) iterator.next(); 
	            if(timeSlot.equals(newTimeSlot)) {
	            	System.out.println("Section already has a timeslot");
	            	flag = true;
	            	break;
	            }
	             
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
		 
		 return flag;
	}
	
	

}
